package org.hydrakyoufeng.oa.configuration;

import java.util.Map;

import javax.sql.DataSource;

import org.hydrakyoufeng.generate.DataBaseModel;
import org.hydrakyoufeng.lang.BeforeValidator;
import org.hydrakyoufeng.sb.core.DayTimer;
import org.hydrakyoufeng.sb.core.SBBeforeValidate;
import org.hydrakyoufeng.sb.doc.ApiGeneraterExecutor;
import org.hydrakyoufeng.sb.doc.ApiGeneraterModel;
import org.hydrakyoufeng.sb.doc.Staticize;
import org.hydrakyoufeng.sb.login.LoginToolkit;
import org.hydrakyoufeng.sb.msg.CodeMsgToolkit;
import org.hydrakyoufeng.sb.upload.UploadController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyConfig {

    @Value("${myconfig.staticPath}")
    private String staticPath;

    @Autowired
    private DataSource dataSource;
    
//    /**
//     * 注意，必须使用这种方式，不然接口会被Mybaits代理
//     * @return
//     */
//	@Bean
//	public MsgSender msgSender() {
//		return new AlyMsgSender();
//	}

    /**
     * 跨域
     *
     * @return
     */
    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*"); // 1允许任何域名使用
        corsConfiguration.addAllowedHeader("*"); // 2允许任何头
        corsConfiguration.addAllowedMethod("*"); // 3允许任何方法（post、get等）
        source.registerCorsConfiguration("/**", corsConfiguration); // 4
        return new CorsFilter(source);
    }

    /**
     * 配置硬盘上的图片上传映射地址
     *
     * @return
     */
    @Bean
    public WebMvcConfigurer uploadImage() {
        WebMvcConfigurer wmc = new WebMvcConfigurer() {
            @Override
            public void addResourceHandlers(ResourceHandlerRegistry registry) {
//				因为静态页面交给nginx去代理，所以这里不配置视频和图片的映射地址
//				registry.addResourceHandler("/uploadImages/**").addResourceLocations("file:" + uploadImagePath);
//				registry.addResourceHandler("/uploadVideos/**").addResourceLocations("file:" + uploadVideoPath);
                registry.addResourceHandler("/static/**").addResourceLocations("file:" + staticPath);
            }
        };
        return wmc;
    }


    @Bean
    public ApiGeneraterExecutor apiGeneraterExecutor() {
        ApiGeneraterExecutor apiGeneraterExecutor = new ApiGeneraterExecutor() {
        	
        	@Override
        	protected void updateBeanMap(String templateName, Map<String, Object> bean) {
//        		bean.put("baseURL", remoteAddr + "/");
//        		bean.put("Authorition", "123456");
        	}
        	
        };
        ApiGeneraterModel apiGeneraterModel = new ApiGeneraterModel();
        DataBaseModel dataBaseModel = new DataBaseModel();
        
        apiGeneraterModel.setDatabaseName("oa");
        apiGeneraterModel.setDataBaseModel(dataBaseModel);
        
        dataBaseModel.setDataSource(dataSource);

        apiGeneraterExecutor.setApiGeneraterModel(apiGeneraterModel);
        apiGeneraterExecutor.setStaticize(new Staticize());

        return apiGeneraterExecutor;
    }
    

    @Bean
    public CodeMsgToolkit codeMsgToolkit() {
        CodeMsgToolkit codeMsgToolkit = new CodeMsgToolkit();
        codeMsgToolkit.initCodeMap("userLogin",null);
        return codeMsgToolkit;
    }


    /**
     * 配置登录校验
     * @return
     */
    @Bean
    public LoginToolkit loginToolkit() {
        LoginToolkit loginToolkit = new LoginToolkit();
        loginToolkit.initCacheMap("ADM", null);
        loginToolkit.initCacheMap("USR", null);
        return loginToolkit;
    }

    @Bean
    public BeforeValidator beforeValidator() {
        return new SBBeforeValidate();
    }
    
    /**
     * 每天凌晨的定时器
     * @return
     */
    @Bean
    public DayTimer dayTimer() {
    	return new DayTimer();
    }
    
    @Bean
	public UploadController uploadControlelr() {
		return new UploadController();
	}
	
//	@Bean
//	public UploadService uploadService() {
//		UploadService service = new UploadService();
//		service.setCacheMap(new ConcurrentCacheMap<>());
//		service.setBufferSize(1024);
//		return service;
//	}

}
